package com.wf.ew.relation.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.relation.biz.CustomBiz;
import com.wf.ew.relation.model.Custom;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-11-08 16:08:48
 * @version ：1.0
 */
@Repository
public class CustomBizimpl extends BaseDao implements CustomBiz {

	@Override
	public String list(PageResult<T> page, Custom custom, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Custom> rm = new BeanPropertyRowMapper<Custom>(Custom.class);
		
		StringBuffer sql = new StringBuffer("select c.id, c.name, c.companyId, c.person, c.phone,"
			+" c.tel, c.type, c.orderNum, c.address, c.createTime, c.createUser, c.updateTime,"
			+" c.updateUser");
			sql.append(",cmy.name companyName,suser.name createName ");
			sql.append(" from we_custom c ");
			sql.append(" inner join sys_user suser on suser.id=c.createUser ");
			sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
			sql.append(" where c.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and c.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(custom)){
			if(Utils.isNotEmpty(custom.getName())){
				sql.append(" and (instr(c.name,:name)>0 or instr(c.person,:name)>0 or instr(c.phone,:name)>0 ) ");
				map.put("name", custom.getName());
			}
			if(Utils.isNotEmpty(custom.getType())){
				sql.append(" and instr(c.type,:type)>0 ");
				map.put("type", custom.getType());
			}
			if(Utils.isNotEmpty(custom.getSort())&&Utils.isNotEmpty(custom.getOrder())){
				sql.append(" order by "+custom.getSort()+" "+custom.getOrder());
			} else {
				sql.append(" order by orderNum asc");
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}
	
	@Override
	public List<Custom> list(Custom custom, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Custom> rm = new BeanPropertyRowMapper<Custom>(Custom.class);
		
		StringBuffer sql = new StringBuffer("select c.id, c.name, c.companyId, c.person, c.phone,"
			+" c.tel, c.type, c.orderNum, c.address, c.createTime, c.createUser, c.updateTime,"
			+" c.updateUser");
			sql.append(",cmy.name companyName,suser.name createName ");
			sql.append(" from we_custom c ");
			sql.append(" inner join sys_user suser on suser.id=c.createUser ");
			sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
			sql.append(" where c.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and c.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
		if(Utils.isNotEmpty(custom)){
			if(Utils.isNotEmpty(custom.getName())){
				sql.append(" and (instr(c.name,:name)>0 or instr(c.person,:name)>0 or instr(c.phone,:name)>0 ) ");
				map.put("name", custom.getName());
			}
			if(Utils.isNotEmpty(custom.getType())){
				sql.append(" and instr(c.type,:type)>0 ");
				map.put("type", custom.getType());
			}
			if(Utils.isNotEmpty(custom.getSort())&&Utils.isNotEmpty(custom.getOrder())){
				sql.append(" order by "+custom.getSort()+" "+custom.getOrder());
			} else {
				sql.append(" order by orderNum asc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Custom getById(Custom custom) {
		RowMapper<Custom> rm = new BeanPropertyRowMapper<Custom>(Custom.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id, c.name, c.companyId, c.person, c.phone,"
			+" c.tel, c.type, c.orderNum, c.address, c.createTime, c.createUser, c.updateTime,"
			+" c.updateUser ");
			sql.append(",cmy.name companyName,suser.name createName ");
			sql.append(" from we_custom c ");
			sql.append(" inner join sys_user suser on suser.id=c.createUser ");
			sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
			sql.append(" where c.del=0 ");
		sql.append("and c.id=:id ");
		map.put("id", custom.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(Custom custom, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_custom (name,companyId,person,phone "
			+",tel,type,address,orderNum, createUser )");
		sql.append("values (:name,:companyId,:person,:phone "
			+",:tel,:type,:address,:orderNum,:createUser )");
		map.put("name", custom.getName());
		map.put("companyId", loginUser.getCompanyId());
		map.put("person", custom.getPerson());
		map.put("phone", custom.getPhone());
		map.put("tel", custom.getTel());
		map.put("type", custom.getType());
		map.put("address", custom.getAddress());
		map.put("orderNum", custom.getOrderNum()==null?null:custom.getOrderNum()+"");
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Custom custom, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_custom set name=:name ");
		sql.append(", person=:person, phone=:phone ");
		sql.append(", tel=:tel,  type=:type, address=:address,orderNum=:orderNum ");
		sql.append(", updateUser=:updateUser, updateTime=now() where id=:id");
		map.put("name", custom.getName());
		map.put("person", custom.getPerson());
		map.put("phone", custom.getPhone());
		map.put("tel", custom.getTel());
		map.put("type", custom.getType());
		map.put("address", custom.getAddress());
		map.put("orderNum", custom.getOrderNum()==null?null:custom.getOrderNum()+"");
		map.put("updateUser", loginUser.getId());
		map.put("id", custom.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Custom custom, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(custom.getIds().length()>0){
			for(String ID : custom.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_custom set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
